package vip.websky.project.user.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.RolePermissionBO;
import vip.websky.project.module.entity.user.RolePermission;
import vip.websky.project.user.dao.RolePermissionDao;

import java.util.List;

/**
 * 角色-权限关联 服务类
 *
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/24-22:16
 **/
@Slf4j
@Service
public class RolePermissionServer extends ServiceImpl<RolePermissionDao, RolePermission> {
    @Autowired
    private RolePermissionDao rolePermissionDao;

    /**
     * 1.创建
     *
     * @param permissionBOS 信息
     * @return boolean
     */
    @KingLog
    public boolean  insertRolePermBatch(List<RolePermissionBO> permissionBOS) {
        List<RolePermission> userRoles = ObjectConvertUtils.copyToList(permissionBOS, RolePermission.class);
        return super.insertBatch(userRoles);
    }

    /**
     * 2.根据条件查找一个
     *
     * @param permissionBO 唯有条件
     * @return userRoleBO
     */
    @KingLog
    public RolePermissionBO selectRolePerm(RolePermissionBO permissionBO) {
        RolePermission rolePermission = ObjectConvertUtils.copyToDest(permissionBO, RolePermission.class);
        rolePermission = rolePermissionDao.selectById(rolePermission.getId());
        return rolePermission != null ? ObjectConvertUtils.copyToDest(rolePermission, RolePermissionBO.class):null;
    }

    /**
     * 3.获取角色列表
     *
     * @param permissionBO 角色信息
     * @return Page<userRoleBO>
     */
    @KingLog
    public Page<RolePermissionBO> selectRolePermList(RolePermissionBO permissionBO) {
        Page<RolePermissionBO> page = new Page<>(permissionBO.getCurPage(), permissionBO.getPageSize());
        EntityWrapper<RolePermission> ew = new EntityWrapper<>();
        return page;
    }

    /**
     * 4.修改角色信息
     *
     * @param permissionBOS 角色
     * @return userRoleBO
     */
    @KingLog
    public boolean updateRolePermBatch(List<RolePermissionBO> permissionBOS) {
        List<RolePermission> rolePermissions = ObjectConvertUtils.copyToList(permissionBOS, RolePermission.class);
        return super.insertOrUpdateBatch(rolePermissions);
    }

    /**
     * 5.删除角色
     *
     * @param permissionBOS id
     * @return boolean
     */
    @KingLog
    public boolean delRolePermBatch(List<RolePermissionBO> permissionBOS) {
        List<RolePermission> rolePermissionList = ObjectConvertUtils.copyToList(permissionBOS, RolePermission.class);
        return super.deleteBatchIds(rolePermissionList);
    }

    @KingLog
    public boolean delRolePermByUserId(RolePermissionBO permissionBO) {
        RolePermission rolePermission = ObjectConvertUtils.copyToDest(permissionBO, RolePermission.class);
        int result = rolePermissionDao.delete(
                new EntityWrapper<RolePermission>().where("User_Id = {0}",rolePermission.getId()));
        return result > 0;
    }
}
